<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>笔试面试_day2</title>
    <style>
        /* 区块引用 */
        .markdown-body blockquote {
            color: rgb(50, 50, 50);
            padding: 5px;
            border: 1px solid rgb(223, 224, 227);
            background-color: rgb(250, 250, 250);
            border-left-width: 5px;
            font-size: 0.5em;
        }

        .markdown-body h1 {
            font-size: 1.2em;
            color: rgb(50, 50, 50);
            letter-spacing: 0.01em;
            padding: 0.5em;
        }

        .markdown-body h2 {
            font-size: 1em;
            color: rgb(50, 50, 50);
            letter-spacing: 0.01em;
            padding: 0.5em;
        }

        .markdown-body h3 {
            font-size: 0.8em;
            color: rgb(50, 50, 50);
            letter-spacing: 0.01em;
            padding: 0.5em;
        }

        .markdown-body h4 {
            font-size: 0.6em;
            color: rgb(50, 50, 50);
            letter-spacing: 0.01em;
            padding: 0.5em;
        }

        .markdown-body h5 {
            font-size: 0.5em;
            color: rgb(50, 50, 50);
            letter-spacing: 0.01em;
            padding: 0.5em;
        }

        /* 无序列表 */
        .markdown-body ol {}

        .markdown-body ol li {
            font-size: 0.6em;
            list-style: inside;
            padding: 0.3em;
            text-decoration: underline;
        }

        .markdown-body p code {
            font-family: 'Source code pro';
            border: 1px solid rgb(223, 224, 227);
            background-color: rgb(250, 250, 250);
            padding: 0px 0.3em;
            font-size: 0.6em;
        }

        .markdown-body p {
            font-size: 0.6em;
            padding: 0.2em;
        }

        /* 代码块 */
        .markdown-body pre {
            border: 1px solid rgb(223, 224, 227);
            background-color: rgb(250, 250, 250);
            padding: 0.5em;

        }

        .markdown-body pre code {
            font-family: 'Source code pro';
            background-color: rgb(250, 250, 250);
            letter-spacing: 0.1em;
            font-size: 0.7em;
            padding: 0px !important;
        }

        /* 无序列表 */
        .markdown-body ul {}

        .markdown-body ul li {
            font-size: 0.6em;
            list-style: inside;
            padding: 0.3em;
            text-decoration: underline;
        }

        .title {
            font-size: 2em;
            border-bottom: 1px solid black;
        }

        .markdown-body {
            font-size: 20px;
            padding: 0em 3em;
        }
    </style>
</head>

<body>
    <div class="markdown-body">
        <h1 class="title">笔试面试_day2</h1>
        <h1 id="1算法">1.算法</h1>
<h3 id="字符串">字符串</h3>
<pre><code class="language-js">var str = &#39;get-element-by-id&#39;;
//把这个字符串改为驼峰式
function f(e){
   e = e.split(&#39;-&#39;);
   var str = &quot;&quot;
   for(var i = 0 ; i &lt; e.length ; i++){
        var item = e[i];
        str += item.slice(0,1).toUpperCase() + item.slice(1)
   }
   return str
}
f(str)
</code></pre>
<hr>
<pre><code class="language-js">function strToObj(str) {
//字符串转对象
    var obj = {}
    var res = obj
    var arr1 = str.split(&#39;,&#39;)
    for (var i = 0; i &lt; arr1.length; i++) {
        var arr2 = arr1[i].split(&#39;.&#39;)
        for(var j = 0; j &lt; arr2.length; j++) {
            res[arr2[j]] = res[arr2[j]] || {}
            res = res[arr2[j]]
        }
        res = obj
    }
    return obj
}
var s1 = &#39;a.b.c.d&#39;
var s2 = &#39;a.e.f&#39;
var s3 = &#39;a.b.r.t&#39;
var str = []
str.push(s1,s2,s3)
console.log(str)
str = str.join()
console.log(str)
console.log(strToObj(str))
</code></pre>
<h3 id="数组扁平化">数组扁平化</h3>
<pre><code class="language-js">function flatten(arr){  //1
   var res = []
   arr.forEach((item)=&gt;{
       if(Array.isArray(item)){
           res = res.concat(flatten(item))
       }else{
           res.push(item)
       }
   })
   return res
}
function flatten(arr){   //2
    return arr.reduce((pre,item,index)=&gt;{
        if(Array.isArray(item)){
            return pre.concat(flatten(item))
        }else{
            return pre.push(item) 
        }
    },[])
}

function flatten(arr){    //3
   var str = arr.toString();
   return str.split(&#39;,&#39;).map((item)=&gt;{
       return Number(item)
   })
}

arr1.flat(Infinity)   //4
</code></pre>
<h3 id="数组去重">数组去重</h3>
<pre><code class="language-js">for(var i = 0 ; i &lt; arr.length ; i++){   //1
    for(var j = i+1 ; j &lt; arr.length ; j++){
        if(arr[i] == arr[j]){
            arr.splice(j,1)
        }
    }
}

var newArr = []   //2
for(var i = 0 ; i &lt; arr.length ; i++){
    var item = arr[i];
    if(newArr.indexOf(item) == -1){
        newArr.push(item)
    }
}

var newArr = arr.filter((item,index)=&gt;{  //3
    return arr.indexOf(item) == index
})

var set = new Set(arr)  //4
Array.from(set)
</code></pre>
<h1 id="2函数柯里化">2.函数柯里化</h1>
<p><strong>好处：1.参数复用  2.提前确定  3.延迟执行</strong></p>
<pre><code class="language-js">var add = function(x,y,z){
    return x+y+z
}
//函数柯里化的函数
function curry(fn){
   return function currify(...args){
       return args.length &gt;= fn.length ? fn.apply(null,args) : currify.bind(null,...args)
   }
}
var curryAdd = curry(add)

console.log(curryAdd(1)(2)(3))
</code></pre>

    </div>
</body>

</html>